/**
 * 
 */
aquire = false;
track = new Track();
watchID = 0;
function onFirstSuccess(position)
{
	alert(position.coords.latitude + "  "+position.coords.longitude);
	document.FormTraining.startTraining.disabled=false;
}

function onFirstError(error)
{
	alert(error.code +"  "+error.message);	
}

function startGPSFix()
{
	if(aquire==false)
	{
		navigator.geolocation.getCurrentPosition(onFirstSuccess, onFirstError, {enableHighAccuracy: true});	
	}
}

function aquireGPSData()
{
	aquire=true;
	watchID = navigator.geolocation.watchPosition(onSuccess, onError, {timeout:5000,enableHighAccuracy: true});
}

function stopGPS()
{
    navigator.geolocation.clearWatch(watchID);
    //sessionStorage.trainingTrack = track;  //HTML5 Storage Method
    //location.href='trainingEvaluation.html';
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function onSuccess(position)
{
    var trackpoint = new Trackpoint();
    trackpoint.setLongitude(position.coords.longitude);
    trackpoint.setLatitude(position.coords.latitude);
    trackpoint.setHeigth(position.coords.altitude);
    if(trackpoint.getHeigth() == null)
    {
        trackpoint.setHeigth("0");
    }
    var now = new Date();
    trackpoint.setTimestamp(now.format("isoUtcDateTime"));
    trackpoint.setVelocity(position.coords.speed);
    
    track.addTrackpoint(trackpoint);
    
	//var body = document.getElementById("b");
	//body.innerHTML += "latitude:" +position.coords.latitude+", longitude: "+position.coords.longitude+", altitude: "+position.coords.altitude+", speed: "+position.coords.speed;
	//document.write("latitude:" +position.coords.latitude+", longitude: "+position.coords.longitude+", altitude: "+position.coords.altitude+", speed: "+position.coords.speed);
}
function onError(error)
{
	alert(error.code+"  "+error.message);
}

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);


function gotFS(fileSystem) 
{
   fileSystem.root.getFile("track.gpx", {"create":true}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) 
{
   fileEntry.createWriter(gotFileWriter, fail);
}

function fail(error) 
{
  console.log(error.code);
}   

function gotFileWriter(writer)
{
    console.log("exportAsGPX-Function");
    //alert(typeof(track));
    writer.write(track.exportAsGPX());
}